<?php

/**
 * 微信配置
 */

namespace app\admin\controller;

use think\Lang;
use think\Validate;
/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class  O2oDistributor extends AdminControl {

    public function _initialize() {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/o2o_distributor.lang.php');
    }

    public function index() {
        $o2o_distributor_model = model('o2o_distributor');
        $condition = array();
        $search_field_value = input('search_field_value');
        $search_field_name = input('search_field_name');
        if ($search_field_value != '') {
            switch ($search_field_name) {
                case 'o2o_distributor_name':
                    $condition['o2o_distributor_name'] = array('like', '%' . trim($search_field_value) . '%');
                    break;
                case 'o2o_distributor_realname':
                    $condition['o2o_distributor_realname'] = array('like', '%' . trim($search_field_value) . '%');
                    break;
                case 'o2o_distributor_phone':
                    $condition['o2o_distributor_phone'] = array('like', '%' . trim($search_field_value) . '%');
                    break;
                case 'o2o_distributor_email':
                    $condition['o2o_distributor_email'] = array('like', '%' . trim($search_field_value) . '%');
                    break;
            }
        }
        $search_state = input('search_state');
        switch ($search_state) {
            case '1':
                $condition['o2o_distributor_state'] = '1';
                break;
            case '0':
                $condition['o2o_distributor_state'] = '0';
                break;
            case '2':
                $condition['o2o_distributor_state'] = '2';
                break;
            case '3':
                $condition['o2o_distributor_state'] = '3';
                break;
            default:
                if (input('verify')) {
                    $condition['o2o_distributor_state'] = array('in', array(2, 3));
                } else {
                    $condition['o2o_distributor_state'] = array('in', array(0, 1));
                }
                break;
        }
        $filtered = 0;
        if ($condition) {
            $filtered = 1;
        }
        $condition = array_merge($condition, array(
            'store_id' => 0,
        ));
        $order = 'o2o_distributor_addtime desc';
        $o2o_distributor_list = $o2o_distributor_model->getO2oDistributorList($condition, '*', 10, $order);
        $this->assign('o2o_distributor_list', $o2o_distributor_list);
        $this->assign('show_page', $o2o_distributor_model->page_info->render());

        $this->assign('search_field_name', trim($search_field_name));
        $this->assign('search_field_value', trim($search_field_value));

        $this->assign('filtered', $filtered); //是否有查询条件

        if (input('verify')) {
            $this->setAdminCurItem('verify');
        } else {
            $this->setAdminCurItem('index');
        }
        return $this->fetch();
    }

    public function add() {
        if (!request()->isPost()) {
            $this->setAdminCurItem('add');
            return $this->fetch('form');
        } else {
            $o2o_distributor_model = model('o2o_distributor');
            $data = $this->post_data();

            $validate_data = array(
                'o2o_distributor_state' => input('post.o2o_distributor_state'),
                'o2o_distributor_realname' => input('post.o2o_distributor_realname'),
                'o2o_distributor_name' => input('post.o2o_distributor_name'),
                'o2o_distributor_password' => input('post.o2o_distributor_password'),
                'o2o_distributor_phone' => input('post.o2o_distributor_phone'),
                'o2o_distributor_email' => input('post.o2o_distributor_email'),
                'o2o_distributor_region_id' => intval(input('post.o2o_distributor_region_id')),
                'o2o_distributor_region_name' => input('post.o2o_distributor_region_name'),
                'o2o_distributor_errand_deposit'=>input('post.o2o_distributor_errand_deposit'),
            );
            $o2o_distributor_validate = validate('o2o_distributor');
            if (!$o2o_distributor_validate->scene('o2o_distributor_register')->check($validate_data)) {
                $this->error($o2o_distributor_validate->getError());
            }

            $condition['o2o_distributor_name'] = $data['o2o_distributor_name'];
            $result = $o2o_distributor_model->getO2oDistributorInfo($condition);
            if ($result) {
                $this->error(lang('o2o_distributor_name_remote'));
            }
            $result = $o2o_distributor_model->addO2oDistributor($data);
            if ($result) {
                $this->log(lang('ds_add') . lang('o2o_distributor') . '[' . $data['o2o_distributor_name'] . ']', 1);
                dsLayerOpenSuccess(lang('ds_common_save_succ'));
            } else {
                $this->error(lang('ds_common_save_fail'));
            }
        }
    }

    public function notice() {
        $id = intval(input('param.id'));
        if (!$id) {
            $this->error(lang('param_error'));
        }
        $o2o_distributor_model = model('o2o_distributor');
        $o2o_distributor_array = $o2o_distributor_model->getO2oDistributorInfo(array('o2o_distributor_id' => $id, 'store_id' => 0));
        if (!$o2o_distributor_array) {
            $this->error(lang('o2o_distributor_empty'));
        }
        if (!request()->isPost()) {
            return $this->fetch('notice');
        } else {
            $content=input('param.content');
            if(!$content){
                $this->error('请填写信息');
            }
            $o2o_distributor_notice_model = model('o2o_distributor_notice');
                                    $o2o_distributor_notice_model->addO2oDistributorNotice(array(
                                        'o2o_distributor_id' => $o2o_distributor_array['o2o_distributor_id'],
                                        'o2o_distributor_name' => $o2o_distributor_array['o2o_distributor_name'],
                                        'o2o_distributor_notice_type' => 0,
                                        'order_id' => 0,
                                        'o2o_distributor_notice_title' => '平台通知',
                                        'o2o_distributor_notice_content' => $content,
                                        'o2o_distributor_notice_add_time' => TIMESTAMP,
                                    ));
            dsLayerOpenSuccess('发送成功',url('O2oDistributor/index',['verify'=>in_array($o2o_distributor_array['o2o_distributor_state'],array(2,3))]));
        }
    }
    
    public function edit() {
        $id = intval(input('param.id'));
        if (!$id) {
            $this->error(lang('param_error'));
        }
        $o2o_distributor_model = model('o2o_distributor');
        $o2o_distributor_array = $o2o_distributor_model->getO2oDistributorInfo(array('o2o_distributor_id' => $id, 'store_id' => 0));
        if (!$o2o_distributor_array) {
            $this->error(lang('o2o_distributor_empty'));
        }
        if (!request()->isPost()) {
            $this->assign('o2o_distributor_array', $o2o_distributor_array);
            return $this->fetch('form');
        } else {

            $data = $this->post_data();

            $validate_data = array(
                'o2o_distributor_realname' => input('post.o2o_distributor_realname'),
                'o2o_distributor_password' => input('post.o2o_distributor_password'),
                'o2o_distributor_phone' => input('post.o2o_distributor_phone'),
                'o2o_distributor_email' => input('post.o2o_distributor_email'),
                'o2o_distributor_region_id' => intval(input('post.o2o_distributor_region_id')),
                'o2o_distributor_region_name' => input('post.o2o_distributor_region_name'),
                'o2o_distributor_errand_deposit'=>input('post.o2o_distributor_errand_deposit'),
            );
            $o2o_distributor_validate = validate('o2o_distributor');
            if (!$o2o_distributor_validate->scene('o2o_distributor_edit')->check($validate_data)) {
                $this->error($o2o_distributor_validate->getError());
            }

            if (isset($data['o2o_distributor_avatar']) && $o2o_distributor_array['o2o_distributor_avatar']) {
                @unlink(BASE_UPLOAD_PATH . DS . ATTACH_O2O_DISTRIBUTOR . DS . $o2o_distributor_array['o2o_distributor_avatar']);
            }
            $result = $o2o_distributor_model->editO2oDistributor($data, array('o2o_distributor_id' => $id, 'store_id' => 0));
            if ($result) {
                $this->log(lang('ds_edit') . lang('o2o_distributor') . '[' . $o2o_distributor_array['o2o_distributor_name'] . ']', 1);
                $content=input('param.content');
                if($content){
                    $o2o_distributor_notice_model = model('o2o_distributor_notice');
                                    $o2o_distributor_notice_model->addO2oDistributorNotice(array(
                                        'o2o_distributor_id' => $o2o_distributor_array['o2o_distributor_id'],
                                        'o2o_distributor_name' => $o2o_distributor_array['o2o_distributor_name'],
                                        'o2o_distributor_notice_type' => 0,
                                        'order_id' => 0,
                                        'o2o_distributor_notice_title' => '平台通知',
                                        'o2o_distributor_notice_content' => $content,
                                        'o2o_distributor_notice_add_time' => TIMESTAMP,
                                    ));
                }
                dsLayerOpenSuccess(lang('ds_common_save_succ'),url('O2oDistributor/index',['verify'=>in_array($o2o_distributor_array['o2o_distributor_state'],array(2,3))]));
            } else {
                $this->error(lang('ds_common_save_fail'));
            }
        }
    }

    public function del() {
        $id = intval(input('param.id'));
        if (!$id) {
            ds_json_encode(10001,lang('param_error'));
        }
        $o2o_distributor_model = model('o2o_distributor');
        $o2o_distributor_array = $o2o_distributor_model->getO2oDistributorInfo(array('o2o_distributor_id' => $id, 'store_id' => 0));
        if (!$o2o_distributor_array) {
            ds_json_encode(10001,lang('o2o_distributor_empty'));
        }
        $result = $o2o_distributor_model->delO2oDistributorInfo(array('o2o_distributor_id' => $id, 'store_id' => 0), array($o2o_distributor_array));
        if (!$result) {
            ds_json_encode(10001,lang('ds_common_del_fail'));
        } else {
            $this->log(lang('ds_del') . lang('o2o_distributor') . '[' . $o2o_distributor_array['o2o_distributor_name'] . ']', 1);
            ds_json_encode(10000,lang('ds_common_del_succ'));
        }
    }

    public function post_data() {
        $data = array(
            'o2o_distributor_realname' => input('post.o2o_distributor_realname'),
            'store_id' => 0,
            'o2o_distributor_phone' => input('post.o2o_distributor_phone'),
            'o2o_distributor_email' => input('post.o2o_distributor_email'),
            'o2o_distributor_remark' => input('post.o2o_distributor_remark'),
            'o2o_distributor_introduce' => input('post.o2o_distributor_introduce'),
            'o2o_distributor_state' => intval(input('post.o2o_distributor_state')),
            'o2o_distributor_region_id' => intval(input('post.o2o_distributor_region_id')),
            'o2o_distributor_region_name' => input('post.o2o_distributor_region_name'),
            'o2o_distributor_errand_deposit'=>input('post.o2o_distributor_errand_deposit'),
        );
        if (request()->action() == 'add') {
            $data['o2o_distributor_name'] = input('post.o2o_distributor_name');
            $data['o2o_distributor_addtime'] = TIMESTAMP;
        }
        if (input('post.o2o_distributor_password')) {
            $data['o2o_distributor_password'] = md5(input('post.o2o_distributor_password'));
        }
        //头像
        if (!empty($_FILES['_pic']['name'])) {
            $upload_file = BASE_UPLOAD_PATH . '/' . ATTACH_O2O_DISTRIBUTOR;
            $file = request()->file('_pic');
            $result = $file->rule('uniqid')->validate(['ext' => ALLOW_IMG_EXT])->move($upload_file);
            if ($result) {
                $data['o2o_distributor_avatar'] = $result->getFilename();
            }
        }
        return $data;
    }

    public function ajax() {
        $o2o_distributor_model = model('o2o_distributor');
        switch (input('param.branch')) {
            /**
             * 品牌名称
             */
            case 'o2o_distributor_name':
                /**
                 * 判断是否有重复
                 */
                $condition['o2o_distributor_name'] = trim(input('param.value'));
                $condition['o2o_distributor_id'] = array('neq', intval(input('param.id')));
                $result = $o2o_distributor_model->getO2oDistributorInfo($condition);
                if (empty($result)) {
                    echo 'true';
                    exit;
                } else {
                    echo 'false';
                    exit;
                }
                break;
        }
    }

    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'index',
                'text' => lang('ds_list'),
                'url' => url('O2oDistributor/index')
            ),
            array(
                'name' => 'verify',
                'text' => lang('ds_verify'),
                'url' => url('O2oDistributor/index', ['verify' => 1])
            ),
            array(
                'name' => 'add',
                'text' => lang('ds_add'),
                'url' => "javascript:dsLayerOpen('" .url('O2oDistributor/add') . "','添加配送员')"
            ),
        );
        return $menu_array;
    }

}
